<?php
include './include/common.inc.php';
include './include/drag.class.php';
require '../include/form.class.php';

global $site_conf;//获取站点全局对象
$site_array = $site_conf->get_site_conf();//获取站点信息
$is_master = $site_array['IS_MASTER'];//是否是主站（1.主站，0.从站）
$site_id = isset( $site_id ) ? $site_id : 'master';//获取站点标识

$drag_db = new drag();
$action = isset( $action ) ? $action : 'manage';
$forward = get_forward( 'drag.php' );

if( $current_user === false )
{
	if( isset( $edufepm ) && $edufepm == 'iframe' )
	{
		$response->alert( '请您登陆后再访问' );
		$response->redirect( $base_dir . 'admin/login.php' );
		$response->execute();
	}
	else
	{
		showmessage( '请您登陆后再访问', $base_dir . 'admin/login.php' );
	}
}

switch( $action )
{
    //创建模板
    case 'create_template':
        if( isset( $_POST['byname'] ) )
        {
            if( empty( $_POST['byname'] ) || strlen( $_POST['byname'] ) > 255 )
			{
				$response->text_alert( 'error','byname','别名不能为空且长度不能大于255个字符' );
			}
            if( empty( $_POST['filename'] ) || strlen( $_POST['filename'] ) > 255 )
			{
				$response->text_alert( 'error','filename','文件名不能为空且长度不能大于255个字符' );
			}
            if( empty( $_POST['url'] ) || strlen( $_POST['url'] ) > 255 )
			{
				$response->text_alert( 'error','url','生成地址不能为空且长度不能大于255个字符' );
			}
            $response->real_execute();
            $params = array(
                'siteid'        =>  $site_id,
                'byname'	    =>  $_POST['byname'],
                'filename'      =>  $_POST['filename'],
                'url'           =>  $_POST['url'],
                'status'        =>  STATUS_NORMAL,
            );
            $res = $drag_db->add_template( $params );
			if( intval( $res ) > 0 )
			{
				$response->alert( '添加成功' );
				$response->redirect( urldecode( $forward ) );
			}
			else
			{
				$response->alert( '添加失败' );
				$response->script( 'self.location = self.location' );
			}
			$response->execute();
            break;
        }
        else
        {
            include template('admin','create_template');
            break;
        }

    //修改模板
    case 'update_template':
        if( isset( $_POST['byname'] ) )
        {
            if( empty( $_POST['byname'] ) || strlen( $_POST['byname'] ) > 255 )
			{
				$response->text_alert( 'error','byname','别名不能为空且长度不能大于255个字符' );
			}
            if( empty( $_POST['filename'] ) || strlen( $_POST['filename'] ) > 255 )
			{
				$response->text_alert( 'error','filename','文件名不能为空且长度不能大于255个字符' );
			}
            if( empty( $_POST['url'] ) || strlen( $_POST['url'] ) > 255 )
			{
				$response->text_alert( 'error','url','生成地址不能为空且长度不能大于255个字符' );
			}
            $response->real_execute();
            $params = array(
                'byname'	    =>  $_POST['byname'],
                'filename'      =>  $_POST['filename'],
                'url'           =>  $_POST['url'],
            );
            $res = $drag_db->edit_template( $params, $templateid );
			if( intval( $res ) > 0 )
			{
				$response->alert( '修改成功' );
				$response->redirect( urldecode( $forward ) );
			}
			else
			{
				$response->alert( '修改失败' );
				$response->script( 'self.location = self.location' );
			}
			$response->execute();
        }
        else
        {
            $templateid = intval( $templateid );
			$template_single = $drag_db->get_template( $templateid );
			if( !isset( $template_single['templateid'] ) )
			{
				showmessage( '要编辑的模板不存在' );
			}
            include template( 'admin', 'update_template' );
            break;
        }

    //删除模板
    case 'delete_template':
        $templateid = intval( $templateid );
		$template_single = $drag_db->get_template( $templateid );
		if( !isset( $template_single['templateid'] ) )
		{
			$response->alert( '要删除的模板不存在' );
			$response->execute();
		}
		$res = $drag_db->del_template( $templateid );
		if( $res == false )
		{
			$response->alert( '删除失败' );
		}
		else
		{
			$response->alert( '删除成功' );
			$response->script( 'self.location=self.location' );
		}
		$response->execute();
        break;

    //生成模板
    case 'build_template':
        $templateid = intval( $templateid );
		$template_single = $drag_db->get_template( $templateid );
		if( !isset( $template_single['templateid'] ) )
		{
			$response->alert( '要生成的模板不存在' );
			$response->execute();
		}
        $html = 'html';
        $forward = make_forward();
        include template( 'drag', $template_single['filename'] );
        break;

    //模板保存及生成静态页
    case 'setting':
        $templateid = intval( $templateid );
		$template_single = $drag_db->get_template( $templateid );
		if( !isset( $template_single['templateid'] ) )
		{
			$response->alert( '要生成的模板不存在' );
			$response->execute();
		}
        $setting = $_COOKIE['list'];
        if( isset($setting) )
        {
            $column = explode( "|", $setting );
            foreach( $column as $c )
            {
                if( !empty( $c ) )
                {
                    $set_type = substr( $c, 1, 1 );
                    $c_ids = substr( $c, 3 );
                    $ids = explode( "@",$c_ids );
                    $n = 0;
                    foreach( $ids as $id )
                    {
                        if( !empty( $id ) )
                        {
                            $n++;
                            $db->simple_update( DB_PRE.'drag_setting', array( 'column_id'=>$set_type, 'listorder'=>$n ), array( 'id'=>$id, 'templateid'=>$templateid ) );
                        }
                    }
                }
            }
            ob_start();
            include template( 'drag', $template_single['filename'] );
            createhtml( APP_ROOT . $template_single['url'] );
            showmessage( "保存成功", "admin/drag.php?action=manage_template" );
        }
        else
        {
            showmessage( "保存失败", HTTP_REFERER );
        }
        break;

    //添加一个模块
    case 'add':
        $module_type = isset( $module_type ) && !empty( $module_type ) ? $module_type : 'other';
        $tab_column = $module_type == 'tab' ? 2 : 0;
        $params = array(
                'column_id'     =>  1,
                'templateid'    =>  $templateid,
                'module_type'   =>  $module_type,
                'module_name'   =>  'temp_' . $module_type,
                'status'        =>  STATUS_NORMAL,
                'tab_column'    =>  $tab_column,
        );
        $res = $drag_db->add( $params );
        if( $module_type == 'tab' )
        {
            $parentid = $db->get_insert_id();
            $child_params = array(
                'parentid'      =>  $parentid,
                'column_id'     =>  1,
                'templateid'    =>  $templateid,
                'module_type'   =>  $module_type,
                'module_name'   =>  'temp_' . $module_type,
                'status'        =>  STATUS_NORMAL,
            );
            for( $i = 0; $i < 2; $i ++ )
            {
                $res = $drag_db->add( $child_params );
            }
        }
        if( intval( $res ) > 0 )
        {
            $response->alert( '添加成功' );
            $response->script( 'self.location = self.location' );
        }
        else
        {
            $response->alert( '添加失败' );
            $response->script( 'self.location = self.location' );
        }
        $response->execute();
        break;

    //修改一个模块
    case 'upd':
        $tab_column = isset( $_POST['tab_column'] ) ? intval( $_POST['tab_column'] ) : 0;
        $tab_column_old = isset( $_POST['tab_column_old'] ) ? intval( $_POST['tab_column_old'] ) : 0;
        if( $tab_column > $tab_column_old )
        {
            $child_params = array(
                'parentid'      =>  $_POST['id'],
                'templateid'    =>  $_POST['templateid'],
                'column_id'     =>  $_POST['column_id'],
                'module_type'   =>  $_POST['module_type'],
                'module_name'   =>  'temp_' . $_POST['module_type'],
                'status'        =>  STATUS_NORMAL,
            );
            for( $i = 0; $i < $tab_column-$tab_column_old; $i ++ )
            {
                $drag_db->add( $child_params );
            }
        }
        $params = array(
            'name'          =>  $_POST['name'],
            'namelen'	    =>  $_POST['namelen'],
            'catid'         =>  empty($_POST['catid']) ? '' : $_POST['catid'] ,
            'background'    =>  $_POST['background'],
            'page'          =>  $_POST['page'],
            'url'           =>  $_POST['url'],
            'tab_column'    =>  $tab_column>$tab_column_old ? $tab_column : $tab_column_old,
        );
        if(empty($_POST['parentid']))
        {
            $params['column_id']=$_POST['column_id'];
            $params['module_type']=$_POST['module_type'];
            $params['module_name']='temp_' . $_POST['module_type'];
        }
        $res = $drag_db->edit( $params, $_POST['id'] );
        if( intval( $res ) > 0 )
        {
            //$response->alert( '修改成功' );
            //$response->script( 'self.location=self.location' );
            showmessage( "修改成功", "admin/drag.php?action=build_template&templateid=".$_POST['templateid'] );
        }
        else
        {
            //$response->alert( '修改失败' );
            //$response->script( 'self.location = self.location' );
            showmessage( "修改失败", "admin/drag.php?action=build_template&templateid=".$_POST['templateid'] );
        }
        $response->execute();
        break;

    //回显一个模块信息
    case 'check':
        $id = $_POST['id'];
        $drag_single = $db->query( "select * from " . DB_PRE . "drag_setting where id=" . $id );
        $form_view = "<form action='admin/drag.php?action=upd' method='post' name='div_form' onsubmit='return r_submit(this);'>
			<table>
				<tr>
				    <td>区块名称</td>
			        <td><input type='text' name='name' value=" . $drag_single[0]['name'] . " ></td>
			    </tr>
                ";
                if( empty($drag_single[0]['parentid']) )
                {
                $form_view .= "<tr>
				    <td>列数</td>
			        <td align='left'><select name='column_id'><option value=''>请选择</option><option value='1'";
                 if( $drag_single[0]['column_id']==1 )
                 {
                    $form_view .= "selected";
                 }
                 $form_view .= ">1列</option><option value='2'";
                 if( $drag_single[0]['column_id']==2 )
                 {
                    $form_view .= "selected";
                 }
                 $form_view .= ">2列</option><option value='3'";
                 if( $drag_single[0]['column_id']==3 )
                 {
                    $form_view .= "selected";
                 }
                 $form_view .= ">3列</option><option value='4'";
                 if( $drag_single[0]['column_id']==4 )
                 {
                    $form_view .= "selected";
                 }
                 $form_view .= ">4列</option></select></td>
			    </tr>
                <tr>
				    <td>模块类型</td>
			        <td align='left'><select name='module_type'><option value=''>请选择</option><option value='list'";
                 if( $drag_single[0]['module_type']=='list' )
                 {
                    $form_view .= "selected";
                 }
                $form_view .= ">列表</option><option value='magic'";
                if( $drag_single[0]['module_type']=='magic' )
                 {
                    $form_view .= "selected";
                 }
                $form_view .= ">幻灯片</option><option value='tab'";
                if( $drag_single[0]['module_type']=='tab' )
                 {
                    $form_view .= "selected";
                 }
                 $form_view .= ">TAB列表</option></select></td>
			    </tr>
                <tr>
				    <td>模块模板</td>
			        <td>" . form::select_drag_template('module_name','module_name',$drag_single[0]['module_name'],'temp' ) . "</td>
			    </tr>";
                }
                $form_view .= "
				<tr>
				    <td>名称长度</td>
			        <td><input type='text' name='namelen'  value=" . $drag_single[0]['namelen'] . " ></td>
			    </tr>
				<tr>
				    <td>背景图片</td>
			        <td><input type='text' name='background' value=" . $drag_single[0]['background'] . " ></td>
			    </tr>
				<tr>
				    <td>栏目ID</td>
			        <td>" . form::select_category('catid','catid','', $drag_single[0]['catid']) . "</td>
			    </tr>
				<tr>
				    <td>行数</td>
			        <td><input type='text' name='page' value=" . $drag_single[0]['page'] . "></td>
			    </tr>
                <tr>
				    <td>链接地址</td>
			        <td><input type='text' name='url' value=" . $drag_single[0]['url'] . "></td>
			    </tr>";
                if( $drag_single[0]['module_type']=='tab' && empty($drag_single[0]['parentid']) )
                {
                    $form_view.= "
                    <tr>
                        <td>TAB列数</td>
                        <td><input type='text' name='tab_column' value=" . $drag_single[0]['tab_column'] . "></td>
                    </tr>";
                }
			 $form_view.="</table>
             <input type='hidden' name='id' value=" . $drag_single[0]['id'] . ">
             <input type='hidden' name='parentid' value=" . $drag_single[0]['parentid'] . ">
             <input type='hidden' name='templateid' value=" . $drag_single[0]['templateid'] . ">
             <input type='hidden' name='tab_column_old' value=" . $drag_single[0]['tab_column'] . ">
         </form>";
        echo $form_view;
        break;

    //删除一个模块
    case 'del':
        $id = intval( $id );
		$module_single = $drag_db->get( $id );
		if( !isset( $module_single['id'] ) )
		{
			$response->alert( '要删除的模块不存在' );
			$response->execute();
		}
		$res = $drag_db->del( $id );
        //如果是TAB删除子TAB时，更新主TAB列数
        if( $module_single['module_type'] == 'tab' && !empty( $module_single['parentid'] ) )
        {
            $parent_single = $drag_db->get( $module_single['parentid'] );
            $tab_column = intval($parent_single['tab_column']) - 1;
            $res = $drag_db->edit( array( 'tab_column' => $tab_column ), $module_single['parentid'] );
        }
		if( $res == false )
		{
			$response->alert( '删除失败' );
		}
		else
		{
			$response->alert( '删除成功' );
			$response->script( 'self.location=self.location' );
		}
		$response->execute();
        break;

    //管理模板
    case 'manage_template':

    default:
        $page = isset( $page ) ? intval( $page ) : 1;
		$page = max( $page, 1 );
        $pagesize = 10;
        $where['siteid'] = $is_master == '1' && $site_id == 'master' ? null :  $site_id;
		$template_manage = $drag_db->get_template_list( $where, $page, $pagesize );
        $template_pages = $drag_db->pages;
        $forward = make_forward();
		include template( 'admin', 'manage_template' );
        break;
}

?>
